var manageauth_href=window.location.href;
$(document).ready(function(){
    var menu_height=$(document).height();
    $("#left-menu").css("height",menu_height+"px");
    console.log(manageauth_href);
    if(!manageauth_href.split("?")[1]){
        //全部公司列表
        $("#all_company").css("display","block");
        $("#company_info").css("display","none");
        $("#person_info").css("display","none");
        all_company();
    }else{
        var href_info=manageauth_href.split("?")[1];
        var href_info_id=href_info.split("&")[0];
        var href_info_user=href_info.split("&")[1];
        if(!href_info_user){
            //公司详情
            $("#all_company").css("display","none");
            $("#company_info").css("display","block");
            $("#person_info").css("display","none");
            company_info();
        }else{
            //个人详情
            $("#all_company").css("display","none");
            $("#company_info").css("display","none");
            $("#person_info").css("display","block");
            person_info();
        }
    }
    //修改信息
    $("#manage_info_change").click(function(){
        $(".info_comall input,select").attr("disabled",false);
        $(".info_perall input").attr("disabled",false);
        $("#manage-wrap input").css("border","1px solid #ccc");
        $("#manage_info_change").css("display","none");
        $("#manage_change_confirm").css("display","block");
        $("#manage_change_cancel").css("display","block");
        click_pic();
    })
    //取消修改信息
    $("#manage_change_cancel").click(function(){
        $(".info_comall input,select").attr("disabled",true);
        $(".info_perall input").attr("disabled",true);
        $("#manage-wrap input").css("border","none");
        $("#manage_info_change").css("display","block");
        $("#manage_change_confirm").css("display","none");
        $("#manage_change_cancel").css("display","none");
    })
    //简称验证
    $("#manageinfo_name").blur(function(){
        get_company_name_check($("#manageinfo_name").val());
    });

    //个人信息修改信息
    $("#person_info_change").click(function(){
        $("#person-wrap input").attr("disabled",false);
        $("#person-wrap input").css("border","1px solid #ccc");
        $("#person_info_change").css("display","none");
        $("#person_change_confirm").css("display","block");
        $("#person_change_cancel").css("display","block");
    })
    //个人信息取消修改信息
    $("#person_change_cancel").click(function(){
        $("#person-wrap input").attr("disabled",true);
        $("#person-wrap input").css("border","none");
        $("#person_info_change").css("display","block");
        $("#person_change_confirm").css("display","none");
        $("#person_change_cancel").css("display","none");
    })
    //添加产品信息
    $("#add_planeinfo").click(function(){
        $("#planeinfo_listall").append('<li><span class="add_planeinfo_num"></span><span class="add_planeinfo_uavnum">飞机编号</span><input class="add_planeinfo_planenum" type="text"><span class="add_planeinfo_plane"></span><button class="add_planeinfo_confirm" type="button">确定</button><button class="add_planeinfo_cancel" type="button">删除</button><button class="add_planeinfo_change" type="button">修改</button><button class="add_planeinfo_info" type="button">详情</button></li>');
        
        $(".add_planeinfo_cancel").on("click",function(){
            //console.log($(this).parent());
            console.log($(this));
            $(this).parent().remove();
            console.log($("#planeinfo_listall"));
            //console.log($(".add_planeinfo_num"));
            //console.log($("#planeinfo_listall"));
            //for(var i in $("#planeinfo_listall li")){
                // console.log($("#planeinfo_listall li")[i]);
            //}
            //$("#planeinfo_listall li")
            //$(".add_planeinfo_num").text()
        });
    });
    
    
});

//左侧菜单始终保持100%高度
$(document).scroll(function(){
    var menu_height=$(document).height();
    $("#left-menu").css("height",menu_height+"px");
});

function topbtn(){
    if($("#topbar-ul").css("display")=="none"){
        $("#topbar-ul").css("display","block");
    }else{
        $("#topbar-ul").css("display","none");
    }
     
}
    //公司列表
function all_company(){
     $.ajax({
        url: '/company_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                console.log(data);
                var strall="";
                var num1=0;
                for(var i in data){
                    if(data[i].is_checked==true){
                        num1+=1;
                        var user_list=data[i].user_list;
                        var str="";
                        console.log(user_list);
                        for(var j in user_list){
                            str+= '<ul><li>'+(parseInt(j)+1)+'</li><li>'+user_list[j].realname+'</li><li>'+user_list[j].position+'</li><li>'+data[i].reg_time+'</li><li><a href="/manageauth?id='+data[i]._id+'&user='+user_list[j].name+'">详情</a></li></ul>'
                        }
                        strall+='<div class="wrap-all"><ul class="company-wrap"><li>'+num1+'</li><li>'+data[i].name+'</li><li>'+data[i].business_field+'</li><li>申请时间'+data[i].reg_time+'</li><li><a href="/manageauth?id='+data[i]._id+'">详情</a></li><li><i class="iconfont icon-xiangxia info-btn"></i></li></ul><div class="person-wrap">'+str+'</div></div>';
                        $("#manage-wrap").html(strall);
                    }
                }
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
    $(document).ajaxComplete(function(){
        var info_btn=$(".info-btn");
        //console.log(info_btn.length);
         for(var i=0;i<info_btn.length;i++){
            info_btn.eq(i).attr("index",i);
            //console.log(info_btn);
            info_btn.eq(i).on("click",info_btn.eq($(this).attr("index")),function(e){
                //alert(0);
                //console.log($(this).attr("index"));
                var num=$(this).attr("index");
                if($(".person-wrap").eq(num).css("display")=="none"){
                    $(".person-wrap").eq(num).css("display","block");
                }else{
                    $(".person-wrap").eq(num).css("display","none");
                }
                e.stopPropagation();
                //onselectstart="return false";
            });
         }
    });
}
//公司详情
function company_info(){
    // $('#licence-img').click(function(){
    //     $('#licbtn').click();
    // });
    // $('#licbtn').change(function(){
    //     var file = this.files[0]; //选择上传的文件
    //     var r = new FileReader();
    //     r.readAsDataURL(file); //Base64
    //     var that=this;
    //     $(r).load(function(){
    //     $('#licence-img').css("border","0");
    //     $('#licence-img span').css("display","none");
    //     $('#licence-image').attr("src",this.result);
    //     $('#licence-image').css("display","block");
    //     });
    // });
    // $('#logo-img').click(function(){
    //     $('#licbtnlogo').click();
    // });
    // $('#licbtnlogo').change(function(){
    //     var file = this.files[0]; //选择上传的文件
    //     var r = new FileReader();
    //     r.readAsDataURL(file); //Base64
    //     var that=this;
    //     $(r).load(function(){
    //     $('#logo-img').css("border","0");
    //     $('#logo-img span').css("display","none");
    //     $('#logo-image').attr("src",this.result);
    //     $('#logo-image').css("display","block");
    //     });
    // });
     $.ajax({
        url: '/company_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                console.log(data);
                var href_info=manageauth_href.split("?")[1];
                var href_info_id=href_info.split("&")[0];
                for(var i in data){
                    if(href_info_id.split("=")[1]==data[i]._id){
                        $("#company_name").attr("href","/manageauth?id="+data[i]._id);
                        $("#company_name").html(data[i].name);
                        $("#manageinfo_name").val(data[i].name);
                        $("#manageinfo_fullname").val(data[i].fullname);
                        $("#manageinfo_address").val(data[i].address);
                        $("#manageinfo_website").val(data[i].website);
                        
                        $("#manageinfo_corporate").val(data[i].corporate);
                        var text=data[i].business_field;
                        $("#manageinfo_field option").each(function(){
                            //console.log($(this).text());
                            //console.log(text);
                            if($(this).text()==text){
                                console.log($(this).text());
                                $(this).attr("selected",true);
                            }
                        });  
                        $("#managecontact_name").val(data[i].contact_name);
                        $("#manageinfo_address").val(data[i].contact_position);
                        $("#managecontact_phone").val(data[i].contact_mobile);
                        $("#managecontact_tax").val(data[i].contact_tax);
                        $("#managecontact_email").val(data[i].contact_email);
                    }
                }
                
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}
//个人详情
function person_info(){
     $.ajax({
        url: '/company_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                var href_info=manageauth_href.split("?")[1];
                var href_info_id=href_info.split("&")[0];
                var href_info_user=href_info.split("&")[1];
                for(var i in data){
                    if(href_info_id.split("=")[1]==data[i]._id){
                         $("#person_companyname").attr("href","/manageauth?id="+data[i]._id);
                         $("#person_companyname").html(data[i].name);
                         var person_user_list=data[i].user_list;
                         for(var j in person_user_list){
                             if(person_user_list[j].name==href_info_user.split("=")[1]){
                                $("#person_name").attr("href","/manageauth?id="+data[i]._id+"&user="+person_user_list[j].name);
                                $("#person_name").html(person_user_list[j].realname);
                                $("#personinfo_name").val(person_user_list[j].name);
                                $("#personinfo_position").val(person_user_list[j].position);
                                $("#personinfo_realname").val(person_user_list[j].realname);
                                $("#personinfo_email").val(person_user_list[j].email);
                                $("#personinfo_phone").val(person_user_list[j].phone);
                             };
                         }
                    }
                }
                
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}
//简称验证
var company_flag=0;
function get_company_name_check(name){
  var company_info = {
    name : name
  };
  $.ajax({
    url: '/company_name_check',
    type: 'get',
    data: company_info,
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        if($("#manageinfo_name").val()==""){
          $("#manageinfo_named").html("*请输入企业简称");
          $("#manageinfo_named").css("display","inline");
          $("#manageinfo_named").css("color","red");
          company_flag=0;
        }else{
          if(data.exist==true){
            $("#manageinfo_named").html("*该公司简称可以使用");
            $("#manageinfo_named").css("display","inline");
            $("#manageinfo_named").css("color","green");
            company_flag=1;
          }else{
            $("#manageinfo_named").html("*该公司简称已被占用");
            $("#manageinfo_named").css("display","inline");
            $("#manageinfo_named").css("color","red");
            company_flag=0;
          }
        }
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function click_pic(){
    $("#licence-img").unbind("click");
    $("#logo-img").unbind("click");
    $('#licence-img').click(function(){
        $('#licbtn').click();
    });
    $('#licbtn').change(function(){
        var file = this.files[0]; //选择上传的文件
        var r = new FileReader();
        r.readAsDataURL(file); //Base64
        var that=this;
        $(r).load(function(){
        $('#licence-img').css("border","0");
        $('#licence-img span').css("display","none");
        $('#licence-image').attr("src",this.result);
        $('#licence-image').css("display","block");
        });
    });
    $('#logo-img').click(function(){
        $('#licbtnlogo').click();
    });
    $('#licbtnlogo').change(function(){
        var file = this.files[0]; //选择上传的文件
        var r = new FileReader();
        r.readAsDataURL(file); //Base64
        var that=this;
        $(r).load(function(){
        $('#logo-img').css("border","0");
        $('#logo-img span').css("display","none");
        $('#logo-image').attr("src",this.result);
        $('#logo-image').css("display","block");
        });
    });
}